$().ready(initPage);

function initPage(){
    testFunc(test1);
    testFunc(test2);
    testFunc(test3);
    testFunc(test4);
    testFunc(test5);
    testFunc(test6);
    testFunc(test7);
}

function test1(){
    var r = shCreate(
        'table#table?yes.mainFields> table:df\',, :<div>,, tid:~d', 34,
        'tr> th/ :header,, <tr',
        'tr>tr td/ :c#,, ',
        'tr>#go td/ :java,,',
        'tr#go>.test|.test2 td/ :c++,,',
        'tr.test2> td/ :python,, <tr',
        'for(/languages)',
        'tr>tr td/ :~s/langName',
        '//efor <table',
        'div> for(/languages)',
        'for(/test)',
        'span/ : ~s/testContent',
        'if(~s/ifCond t) span/ : ~s //eif', 'test if cond',
        'if(~d 3) span/ : ~s //eif', 3, 'test if cond 2',
        'if(~d 3) span/ : ~s //eif', 4, 'test if cond 3, this should not be display',
        '//efor',
        '//efor <div'
        );

    r.languages = [
    {
        langName: 'ruby',
        test:
        [{
            testContent: 'whats on'
        },{
            testContent: 'nested for',
            ifCond: 't'
        }]
    },
    {
        langName: 'prolog',
        test:
        [{
            testContent: 'nested 2'
        }]
    },
    {
        langName:'untitled'
    }
    ];

    var s = shHtml(r);
    return {
        test:'ok',
        res:s,
        msg:'The first version of testing'
    };
}

function test2(){
    var sh = shCreate('table> tr> ',
        'th/ :', 'product',
        'th/ :', 'price',
        'th/ :', 'provider',
        '<tr');

    for (var tr = 0;  tr < 1000; tr++) {
        shPush(sh, 'tr>tr pid:', tr,
            'td/ :', 'product #'.concat(tr),
            'td/ :', '$'.concat(Math.getRandomInt(0, 100)),
            'td/ :', 'provider #'.concat(Math.getRandomInt(0, 10))
            );
    }
    var s = shHtml(sh);

    return {
        test:'ok',
        res:s,
        msg:'1000 records'
    };
}

function test3(){
    var sh = shCreate('table> tr> ',
        'th/ :', 'product',
        'th/ :', 'price',
        'th/ :', 'provider',
        '<tr');

    for (var tr = 0;  tr < 1000; tr++) {
        shPush(sh, ['tr>tr pid:', tr,
            'td/ :', 'product #'.concat(tr),
            'td/ :', '$'.concat(Math.getRandomInt(0, 100)),
            'td/ :', 'provider #'.concat(Math.getRandomInt(0, 10))]
            );
    }
    var s = shHtml(sh);
    return {
        test:'ok',
        res:s,
        msg:'1000 records using array'
    };
}

function test4(){
    var sh = shCreate('for(/tables) div> ',
        // start table
        'table> ',
        'tableId: /tableId',
        'tr> th/ :/header <<',
        'for(/tableContent)',
        // if the content is not fixed
        '  if(/isFixed f)',
        '  tr> td/ :/content <tr',
        '  //eif',
        // if the content is fixed
        '  if(/isFixed t)',
        '  tr> td/ :fixed,, <tr',
        '  //eif',
        '//efor',
        '<table',
        'if(~s t) hr/ //eif', 't',
        '<div',
        '//efor'
        );
    //    sh['tables'] ;
    var tables = sh['tables'] = [];
    for(var i = 0; i < 30; i++){
        tables[i] = {};
        tables[i]['tableId'] = i;
        tables[i]['header'] = 'the header ' + i;
        var tableContent = tables[i]['tableContent'] = [];
        for(var j = 0; j < 100; j++){
            tableContent[j] = {};
            tableContent[j]['isFixed'] = Math.random() > 0.5? 't':'f';
            tableContent[j]['content'] = 'this is unfixed value #' + j;
        }
    }
    var s = shHtml(sh);
    return {
        test:'ok',
        res:s,
        msg:'for and if ...'
    };
}

function test5(){
    var sh = shCreate('div>div :div #1,,',
        'div>#div3 &div #2&',
        'div#div3> :div #3,, divId:', 3,
        '<',
        'div/ :div #4,,',
        'div/ :div #5,, <',
        'div> :div #6,, <1',
        'div> div/ :nested div in #7,, <2',
        'div.div8> div/ :nested div in #8,, <.div8',
        'div>.div10|#divNo :div #9,,',
        'div.div10>table|div :div #10,,',
        'div> div/ :nested div in #11,, <<'
        );

    var s = shHtml(sh);
    return {
        test:'ok',
        res:s,
        msg:'tag closing ...'
    };
}

function test6(){
    var sh = shCreate(
        shCreate('div> span/ &this is a tpl test&'),
        shCreate('div> span/ :another tpl,,'),
        'div> /shTpl < div> /htmlTpl <'
        );
    sh['shTpl'] = shCreate('a/ href:http://www.google.com,, &to google&');
    sh['htmlTpl'] = '<font size="50">some fixed text</font>';

    var s = shHtml(sh);
    return {
        test:'ok',
        res:s,
        msg:'tpl testing'
    }
}

function test7(){
    var sh = shCreate(
        'for(/for1) div>div :/content //efor',
        'if(~d/test 2) div> :whats going on true,, < //eif',
        'if(~d/test 3) div> :whats going on false,, < //eif',
        'for(/for2) div> : /content ',
        '  for(/subFor) div> /content divId: ~s/divId fixedVal: ~d <div :', 23, 'html text',
        '  //efor',
        '< //efor'
        );

    sh['test'] = 2;

    sh['for1'] = [
    {
        content:'for for 1'
    },

    {
        content:'for wfwef'
    },

    {
        content:'for fefawefag'
    },

    {
        content:'for fqgrqege'
    },

    {
        content:'for bebdf1'
    },

    {
        content:'for 452h45h'
    },

    {
        content:'for bwerbw'
    },

    {
        content:'for dbwrwb'
    },

    {
        content:'for bwebwd'
    },

    {
        content:'for berb45'
    },

    {
        content:'for qebrd'
    },

    {
        content:'for berebq'
    },

    {
        content:'for qebqebq'
    },

    {
        content:'for cbe'
    },

    {
        content:'for bqebqe'
    },

    {
        content:'for 34vqeq'
    }
    ];

    var i = 0;
    sh['for2'] = [
    {
        content: '#' + (i++),
        subFor:[
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        }
        ]
    },
    {
        content: '#' + (i++),
        subFor:[
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        }
        ]
    },
    {
        content: '#' + (i++),
        subFor:[
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        }
        ]
    },
    {
        content: '#' + (i++),
        subFor:[
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        }
        ]
    },
    {
        content: '#' + (i++),
        subFor:[
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        }
        ]
    },
    {
        content: '#' + (i++),
        subFor:[
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        }
        ]
    },
    {
        content: '#' + (i++),
        subFor:[
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        }
        ]
    },
    {
        content: '#' + (i++),
        subFor:[
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        },
        {
            content: ' sub #' + i++,
            divId: i++
        }
        ]
    }
    ];

    var s = shHtml(sh);
    return {test: 'ok', res:s, msg: 'Testing for and if'};
}